using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class TimeReward : MonoBehaviour
{
	public static TimeReward instance;

	public int rewardItemCoolTime;

	public GameObject GO_TimeReward;

	public Image IMG_TimeReward;

	public Animator AT_TimeReward;

	private int resulttimestamp;

	private void Awake()
	{
		instance = this;
	}

	public void Init()
	{
		if (PlayerPrefs.GetInt("getRewardItemTime") == 0)
		{
			SetRewardItemTime();
		}
		StartCoroutine(IE_ShowRewardTime());
	}

	public void SetRewardItemTime()
	{
		PlayerPrefs.SetInt("getRewardItemTime", Calc.GetRealTime() + rewardItemCoolTime);
	}

	private IEnumerator IE_ShowRewardTime()
	{
		while (true)
		{
			resulttimestamp = PlayerPrefs.GetInt("getRewardItemTime") - Calc.GetRealTime();
			if (resulttimestamp <= 0 && !GO_TimeReward.activeSelf && CanvasList.instance.GO_PropsForTitle[0].activeSelf)
			{
				IMG_TimeReward.sprite = Sprites.instance.gemSpriteList[Random.Range(0, Sprites.instance.gemSpriteList.Length)];
				GO_TimeReward.SetActive(true);
				AT_TimeReward.enabled = true;
			}
			yield return new WaitForSecondsRealtime(1f);
		}
	}

	public void GetTimeReward()
	{
		StartCoroutine(IE_GetTimeReward());
	}

	private IEnumerator IE_GetTimeReward()
	{
		AT_TimeReward.Play("TimeRewardHide", -1, 0f);
		PlayerPrefs.SetInt("playerGem", PlayerPrefs.GetInt("playerGem") + 1);
		Audio.instance.ui[2].Play();
		SetRewardItemTime();
		yield return new WaitForSeconds(1.5f);
		GM.instance.CheckMoreUnlockingEquips();
		AT_TimeReward.enabled = false;
		GO_TimeReward.SetActive(false);
	}

	public void ShowBTN_PlayVideoRewardAd(bool swt)
	{
		if (swt)
		{
			CanvasList.instance.GO_PlayVideoRewardAd.SetActive(true);
			/* if (AdMobCtrl.instance.rewardBasedVideo.IsLoaded())
			{
				CanvasList.instance.GO_PlayVideoRewardAd.SetActive(true);
			} */
		}
		else
		{
			CanvasList.instance.GO_PlayVideoRewardAd.SetActive(false);
		}
	}

	public void ShowVideoRewardAd()
	{
		CanvasList.instance.GO_PlayVideoRewardAd.SetActive(false);
		//AdMobCtrl.instance.ShowRewardVideo();
		AdManager.Instance.ShowVideo(delegate
		{
			GetVideoRewardAdGems();
		});
	}

	public void GetVideoRewardAdGems()
	{
		StartCoroutine(IE_GetVideoRewardAdGems());
	}

	private IEnumerator IE_GetVideoRewardAdGems()
	{
		CanvasList.instance.Panel_Equip_Toggle();
		AddMoney.addMoney = 20;
		PlayerPrefs.SetInt("playerGem", PlayerPrefs.GetInt("playerGem") + 20);
		UpdateMoney.StartUpdateMoney();
		yield return new WaitForSeconds(0.1f);
		CanvasList.instance.GO_ShowVideoRewardGems.SetActive(true);
		Audio.instance.sfx[25].Play();
		yield return new WaitForSeconds(2f);
		GM.instance.CheckMoreUnlockingEquips();
	}
}
